﻿using System;
using System.Collections.Generic;
using System.Windows;
using Microsoft.Phone.Controls;

namespace BalanceManager
{
    public enum ViewType
    {
        Main,
        AddFundFlow,
        Items,
        History
    }

    public class ApplicationController
    {
        private static ApplicationController instance;
        private readonly Dictionary<ViewType, UriInfo> views;
        private IDictionary<string, object> navigationParameters = new Dictionary<string, object>();
        private IDictionary<string, object> applicationState = new Dictionary<string, object>();

        private class UriInfo
        {
            public UriInfo(string uriString, UriKind uriKind)
            {
                UriString = uriString;
                UriKind = uriKind;
            }

            public string UriString { get; private set; }
            public UriKind UriKind { get; private set; }
        }

        ApplicationController()
        {
            views = new Dictionary<ViewType, UriInfo>();

            Register(ViewType.Main, new UriInfo("/MainPage.xaml", UriKind.Relative));
            Register(ViewType.AddFundFlow, new UriInfo("/Views/AddFundFlow.xaml", UriKind.Relative));
            Register(ViewType.Items, new UriInfo("/Views/ItemsView.xaml", UriKind.Relative));
            Register(ViewType.History, new UriInfo("/Views/HistoryView.xaml", UriKind.Relative));
        }

        public static ApplicationController Default
        {
            get
            {
                if (instance == null)
                    instance = new ApplicationController();

                return instance;
            }
        }

        void Register(ViewType type, UriInfo address)
        {
            if (views.ContainsKey(type)) //update
            {
                views[type] = address;
                return;
            }

            views.Add(type, address); //add
        }

        void UnRegister(ViewType type)
        {
            if (views.ContainsKey(type))
                views.Remove(type);
        }

        public IDictionary<string, object> NavigationParameters
        {
            get { return navigationParameters; }
            set { navigationParameters = value ?? new Dictionary<string, object>(); }
        }

        public IDictionary<string, object> ApplicationState
        {
            get { return applicationState; }
            set { applicationState = value ?? new Dictionary<string, object>(); }
        }

        public void NavigateTo(ViewType type)
        {
            var address = new Uri(views[type].UriString, UriKind.Relative);
            // page  = ((PhoneApplicationFrame)Application.Current.RootVisual).Content as PhoneApplicationPagea
            var root = Application.Current.RootVisual as PhoneApplicationFrame;
            root.Navigate(address);
        }

        public void RemoveBackEntry()
        {
            PhoneApplicationFrame root = Application.Current.RootVisual as PhoneApplicationFrame;
            root.RemoveBackEntry();
        }

        public void GoBack()
        {
            PhoneApplicationFrame root = Application.Current.RootVisual as PhoneApplicationFrame;
            root.GoBack();
        }
    }
}
